package com.like.fragment;


import android.view.View;
import android.widget.EditText;

import com.like.entity.GeneralResponse;
import com.like.entity.RealNameReq;
import com.like.hospitalworld.R;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.ResponseCode;
import com.like.network.RetrofitUtil;
import com.like.network.UserService;
import com.like.utils.Constants;
import com.like.widget.MyToast;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Or;
import com.mobsandgeeks.saripaar.annotation.Order;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.fragment_rnstaff)
public class RNStaffFragment extends BaseFragment implements Validator.ValidationListener {

    @ViewInject(R.id.wwid)
    @Order(0)
    @NotEmpty(message = "请输入WWID")
    private EditText mWwid;

    private Validator mValidator;
    private boolean mIsPass = false;
    private UserService mUserService;

    @Override
    protected void onCreateView() {
        super.onCreateView();
        mValidator = new Validator(this);
        mValidator.setValidationListener(this);
        mUserService = RetrofitUtil.getService(UserService.class);
    }

    @Event(R.id.auth)
    private void authClick(View v) {
        mValidator.validate();
        if (!mIsPass)
            return;
        String wwid = mWwid.getText().toString();
        RealNameReq req = new RealNameReq(m.getLoginResp().userID, RealNameReq.USER_TYPE_STAFF, wwid);
        Call<GeneralResponse<Object>> call = mUserService.setRealName(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<Object>>() {
            @Override
            public void onResponse(Call<GeneralResponse<Object>> call, Response<GeneralResponse<Object>> response) {
                if (!response.isSuccessful())
                    return;
                GeneralResponse resp = response.body();
                if (resp.result == ResponseCode.SUCCESS) {
                    MyToast.showBlueCircleToast(m.mContext, "认证成功");
                    getActivity().finish();
                } else {
                    MyToast.showBlueCircleToast(m.mContext, "认证失败");
                }
            }

            @Override
            public void onFailure(Call<GeneralResponse<Object>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    @Override
    public void onValidationSucceeded() {
        mIsPass = true;
    }

    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        mIsPass = false;
        String msg = errors.get(0).getCollatedErrorMessage(m.mContext);
        MyToast.showBlueCircleToast(m.mContext, msg);
    }
}
